//
//  MineController.m
//  traveling
//
//  Created by xiaoR on 16/3/27.
//  Copyright © 2016年 family. All rights reserved.
//

#import "MineController.h"
#import "HomePageViewController.h"
#define ColorDefine RGBA(255, 255, 255, .8)

@interface MineController () <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate> {
    UIButton * _image;
    UITextField * _name;
    UITextField * _email;
    UITextField * _sign;
    UITextField * _password;
    UITextField * _code;
    UILabel * _phone;
    UIView * _backView;
    BOOL canEdit;
}

@end

@implementation MineController

- (void)viewDidLoad {
    [super viewDidLoad];
    canEdit = NO;
    [self setNavigationBar];
    [self createViews];
    [self createBackView];
    [self setValue:@[@"Traveller", @"iamataveller@trv.com", @"i love the world,i love every thing"]];
    
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(notice:) name:@"123" object:nil];
}

- (void)createBackView {
    _backView = [[UIView alloc] initWithFrame:self.view.bounds];
    _backView.backgroundColor = RGBA(0, 0, 0, .8);
    _backView.alpha = 0;
    _backView.clipsToBounds = YES;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(10, 170, ScreenWidth-20, 160)];
    view.layer.cornerRadius = 5.f;
    view.layer.borderColor = [UIColor whiteColor].CGColor;
    view.layer.borderWidth = .5f;
    [_backView addSubview:view];
    NSArray * arr = @[@"昵  称:", @"邮  箱:", @"签  名:", @"手  机:", @"旧密码/验证码:", @"新密码:"];
    int a = 100;
    for (int i = 0; i < arr.count; i ++) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(((i==4)||(i==5))?20:80, a, ((i==4)||(i==5))?110:50, 20)];
        label.font = FontScale(16);
        label.textColor = ColorNavigation;
        label.text = arr[i];
        label.textAlignment = NSTextAlignmentRight;
        [_backView addSubview:label];
        a += (i==2)?40:20;
        a += (i==3)?20:0;
        a += (i>3)?10:0;
    }
    
    _phone = [[UILabel alloc] initWithFrame:CGRectMake(140, 180, ScreenWidth - 160, 20)];
    _phone.textColor = RGBA(255, 255, 255, .8);
    _phone.text = @"18337123456";
    _code = [[UITextField alloc] initWithFrame:CGRectMake(140, 220, ScreenWidth - 160, 20)];
    _code.placeholder = @"输入原密码或验证码";
    _code.backgroundColor = ColorDefine;
    [_backView addSubview:_code];
    _password = [[UITextField alloc] initWithFrame:CGRectMake(140, 250, ScreenWidth - 160, 20)];
    _password.placeholder = @"输入新密码以修改密码";
    _password.backgroundColor = ColorDefine;
    [_backView addSubview:_password];
    [_backView addSubview:_phone];
    
    UIButton * enter = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2, 20, ScreenWidth/3, 40)];
    enter.backgroundColor = ColorNavigation;
    [enter setTitle:@"确定更改" forState:UIControlStateNormal];
    [enter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    enter.layer.cornerRadius = 5.f;
    enter.layer.masksToBounds = YES;
    [enter addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:enter];
    UIButton * enterPwd = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-90, 180, 70, 20)];
    enterPwd.backgroundColor = ColorNavigation;
    [enterPwd setTitle:@"发送短信" forState:UIControlStateNormal];
    enterPwd.titleLabel.font = FontScale(14);
    [enterPwd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    enterPwd.layer.cornerRadius = 3.f;
    enterPwd.layer.masksToBounds = YES;
    enterPwd.alpha = .8;
    [enterPwd addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:enterPwd];
    UIButton * sendCode = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-50, 280, 100, 30)];
    sendCode.backgroundColor = ColorNavigation;
    [sendCode setTitle:@"修改密码" forState:UIControlStateNormal];
    [sendCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sendCode.layer.cornerRadius = 3.f;
    sendCode.layer.masksToBounds = YES;
    sendCode.alpha = .8;
    [sendCode addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:sendCode];
    
    
    [self.view addSubview:_backView];
}

- (void)endEdit {
    [_name resignFirstResponder];
    [_email resignFirstResponder];
    [_sign resignFirstResponder];
    [_code resignFirstResponder];
    [_password resignFirstResponder];
//    [self resignFirstResponder];
    canEdit = 0;
    [UIView animateWithDuration:.5f animations:^{
        _backView.alpha = 0;
        _name.borderStyle = UITextBorderStyleNone;
        _email.borderStyle = UITextBorderStyleNone;
        _sign.borderStyle = UITextBorderStyleNone;
        _code.borderStyle = UITextBorderStyleNone;
        _password.borderStyle = UITextBorderStyleNone;
        
    }];
}

- (void)notice:(NSNotificationCenter *)center {
    canEdit = 1;
    [UIView animateWithDuration:.5f animations:^{
        _backView.alpha = 1;
        _name.borderStyle = UITextBorderStyleRoundedRect;
        _email.borderStyle = UITextBorderStyleRoundedRect;
        _sign.borderStyle = UITextBorderStyleRoundedRect;
        _code.borderStyle = UITextBorderStyleRoundedRect;
        _password.borderStyle = UITextBorderStyleRoundedRect;
        
    }];
}

- (void)setValue:(NSArray *)arr {
    if (!_name) {
        _name = [[UITextField alloc] initWithFrame:CGRectMake(140, 90, ScreenWidth - 160, 30)];
        _name.textColor = ColorNavigation;
        _name.font = FontScale(22);
        _name.delegate = self;
        _name.backgroundColor = ColorDefine;
        _email = [[UITextField alloc] initWithFrame:CGRectMake(140, 120, ScreenWidth - 160, 20)];
        _email.textColor = RGBA(0, 0, 0, .7);
        _email.font = FontScale(12);
        _email.delegate = self;
        _email.backgroundColor = ColorDefine;
        _sign = [[UITextField alloc] initWithFrame:CGRectMake(140, 140, ScreenWidth - 160, 20)];
        _sign.textColor = RGBA(0, 0, 0, .9);
        _sign.font = FontScale(14);
        _sign.delegate = self;
        _sign.backgroundColor = ColorDefine;
        [self.view addSubview:_name];
        [self.view addSubview:_email];
        [self.view addSubview:_sign];
    }
    _name.text = arr[0];
    _email.text = arr[1];
    _sign.text = arr[2];
}

- (void)createViews {
    _image = [[UIButton alloc] initWithFrame:CGRectMake(10, 74, 120, 120)];
    _image.layer.cornerRadius = 60;
    _image.layer.masksToBounds = YES;
    [_image addTarget:self action:@selector(selectAction) forControlEvents:UIControlEventTouchUpInside];
    [_image setImage:[UIImage imageNamed:@"place"] forState:UIControlStateNormal];
    [self.view addSubview:_image];
    NSArray * arr = @[@"我的收藏", @"感兴趣的城市", @"我的历史", @"发现美妙"];
    for (int i = 0; i < arr.count; i++) {
        NSString * str = arr[i];
        UILabel * lable =[Factory createLabelWithTitle:arr[i] frame:CGRectMake(15 + i % 2 * (ScreenWidth / 2), _image.bottom + 20 + i / 2 * 80, str.length * 15 , 20) textColor:RGBA(1, 1, 1, .6) fontSize:14.f];
        [self.view addSubview:lable];
        UIImageView * subImage = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth / 2 - 15 - 50) + ScreenWidth / 2 * (i % 2), _image.bottom + 35 + i / 2 * 80, 50, 50)];
        subImage.layer.cornerRadius = 25;
        subImage.layer.masksToBounds = YES;
        subImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i + 12]];
        [self.view addSubview:subImage];
        UIButton * button = [Factory createButtonWithTitle:@"" frame:CGRectMake(10 + i % 2 * (ScreenWidth / 2), _image.bottom + 20 + i / 2 * 80, ScreenWidth / 2, 70) backgroundColor:[UIColor clearColor] target:self selector:@selector(buttonAction:)];
        button.tag = i;
        [self.view addSubview:button];
    }
    ViewHLine(0, _image.bottom + 5, ScreenWidth)
    ViewHLine(0, _image.bottom + 15, ScreenWidth)
    ViewHLine(0, _image.bottom + 95, ScreenWidth)
    ViewHLine(0, _image.bottom + 175, ScreenWidth)
    [self.view addSubview:[Factory createViewWithBackgroundColor:RGB(216, 216, 216) frame:CGRectMake(ScreenWidth / 2, _image.bottom + 15, .5, 160)]];
}

- (void)buttonAction:(UIButton *)button {
    NSArray * arr = @[@"myCloudPageController", @"myPlantController", @"myHistoryController", @"myCompanyController"];
    Class subClass = NSClassFromString(arr[button.tag]);
    UIViewController * trends = [subClass new];
    [trends setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:trends animated:YES];
    
}

- (void)setNavigationBar {
    [self createNavigationBar:9];
    self.back.text = @"我的";
    [self.buttonA setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
}

- (void)rightButtonAction {
    [HomePageViewController showRightViewController];
}

#pragma mark deldgate 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return canEdit;
}

#pragma mark 选取图片
- (void)selectAction {
    NSMutableArray * arr = [NSMutableArray new];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        [arr addObject:@"相机"];
    }
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        [arr addObject:@"图库"];
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
        [arr addObject:@"相片库"];
    }
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"照相机",@"相册", nil];
    [actionSheet showInView:self.view];
}

#pragma mark - actionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0://相机
        {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                //实例化一个对象
                UIImagePickerController *imagePicker =[[UIImagePickerController alloc]init];
                //设置资源类型
                imagePicker.sourceType     =UIImagePickerControllerSourceTypeCamera;
                //设置代理
                imagePicker.delegate       = self;
                imagePicker.allowsEditing  = YES;
                //进行模态跳转
                [self presentViewController:imagePicker animated:YES completion:nil];
            }
        }
            break;
        case 1://相册
        {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                UIImagePickerController *imagePicker =[[UIImagePickerController alloc]init];
                imagePicker.sourceType      =UIImagePickerControllerSourceTypePhotoLibrary;
                imagePicker.delegate        = self;
                imagePicker.allowsEditing   = YES;
                [self presentViewController:imagePicker animated:YES completion:nil];
            }
        }
            break;
        default:
            break;
    }
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    // 模态跳转回来
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
//点击确定之后
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage * image = [[UIImage alloc] init];
    image = info[@"UIImagePickerControllerEditedImage"];
    NSData * data = UIImagePNGRepresentation(image);
    UIImage * imageD = [UIImage imageWithData:data];
    [_image setImage:imageD forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end